<?php

require_once APPLICATION_PATH . '\models\Home.php';
require_once APPLICATION_PATH . '\models\Users.php';
require_once APPLICATION_PATH . '\models\PublishSubscribe.php';

/**
 * This is the home page of our application, with 2 cases of use:
 * - when the user is not logged, he can see 2 links: login & register
 * - when he is logged, he can see his homepage with his timeline
 * 
 * @author Christophe Sourisse
 */
class IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    /**
     * If the user is logged, he is redirected towards his home page.     * 
     */
    public function indexAction() {
        $session = Zend_Registry::get("my_session_namespace");
        if (isset($session->user))
            header('Location: index/home');
    }

    /**
     * Home page of the logged user, where he can see his topics, his followers,
     * the messages he has posted, the news of his topics, etc.
     */
    public function homeAction() {        
        
        // Get the session from the registry
        $session = Zend_Registry::get("my_session_namespace");

       // Pass user data to the view
       $this->view->userID = $session->user["id"];
       $this->view->postsFromTopics = Model_Home::getNewMessages_sorted($session->user["topics"]);
       $this->view->postsFromCurrentUser = Model_Home::getUserPosts_sorted($session->user["id"]);
       $this->view->topics = Model_Users::getUsers($session->user["topics"]);
       $this->view->subscribers = Model_PublishSubscribe::getSubscribers($session->user["id"]);
       
       // Pass to the view the form used to share new post
       $form = new Application_Form_Publish(
               "Write a new post?",
               "Share!"
       );
       $form->setAction("javascript:publish();");
       $this->view->newPostForm = $form;
       
       // Add the Javascript file
       $this->view->headScript()->appendFile('/WhatsUpProject/public/js/publishSubscribe.js');
       
       // Set Page's title
       $this->view->headTitle("What's Up :: Home");   
    }

}